import { createRouter, createWebHistory } from 'vue-router'
import {createMSG} from "@/utils/common/common"

const routes = [
  {
    path:'/',
    name:'login',
    component:()=>import("@/views/Login/index.vue"),
  },
  {
    path:'/homepage',
    name:'homepage',
    redirect:"/func",
    component:()=>import("@/views/MainPage/index.vue"),
    children:[
      {
        path:'/func',
        name:'func',
        component:()=>import("@/views/MainPage//Component/Func.vue"),
      },
      {
        path:'/user',
        name:'user',
        component:()=>import("@/views/UserManage/index.vue"),
        children:[
          {
            path:'info',
            name:'info',
            component:()=>import("@/views/UserManage/Components/Info.vue")
          },
          {
            path:'manage',
            name:'manage',
            component:()=>import("@/views/UserManage/Components/Manage.vue")
          },
          {
            path:'mission',
            name:'mission',
            component:()=>import("@/views/UserManage/Components/Mission.vue")
          }
        ]
      }
    ],
  },
  {
    path:'/userManage',
    name:'userManage',
    component:()=>import("@/views/MainPage/index.vue"),
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from,next)=>{

  const verify = JSON.parse(localStorage.getItem('jzcyfh'))

  if (to.path === '/'){
    if (verify){
      router.push('/homepage')
    } else {
      next()
    }
  }

  if (to.path !== '/'){
    if (!verify){
      createMSG('请登录后查看本系统','warning','false')
      return router.push('/')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
